/**
 * bugs
 * opera: click hot key to edit label
 * ie: on ESC, all labels ESC
 */
if (inweo.get("inweo.widgets.label") != true) {
	inweo.set("inweo.widgets.label", true);
	inweo.widgets.label = inweo.core.widget.extend({
		disable: function(bool) {
			if (bool == true) {
				this.widget.setProperty("disabled", "true");
				this.widget.addClass("labelDisabled");
				this.widget.addClass("widgetUnselectable");
			} else if (bool == false) {
				this.widget.setProperty("disabled", "false");
				this.widget.removeClass("labelDisabled");
				this.widget.removeClass("widgetUnselectable");
			} else {
				return new Boolean(this.widget.getProperty("disabled"));
			}
		},
		edit: function() {
			if ((this.disable() == true)
				|| ($type(this.widget.getElement("input")) == "element")) {
				return;
			}

			var self = this;
			var text = this.text();
			var width = this.widget.offsetWidth;
			this.widget.addClass("labelEdit");
			this.span.setText("");

			var input = new Element("input");
			input.addClass("widgetFocused");
			input.setProperty("type", "text");
			input.setStyle("width", width);
			input.injectInside(this.span);
			input.setProperty("value", text);
			input.focus();
			input.select();

			var inputEvent = new inweo.core.event(input);
			inputEvent.addEvent("keydown", function(event) {
				var input = self.widget.getElement("input");
				if ($type(input) != "element") {
					return;
				}
				if (event.key == "esc") {
					self.text(text);
				}
				if (event.key == "enter") {
					self.text(input.getProperty("value"));
				}
				if ((event.key == "enter") || ((event.key == "esc"))) {
					self.widget.removeClass("labelEdit");

					var input = new Element("input");
					input.setStyle("display", "none");
					input.injectInside(document.body);
					try {
						input.focus();
					} catch(e) { /* ie error */ }
					input.remove();

					inputEvent.removeEvent(event);
				}
			});
		},
		editable: function(key) {
			this.editKey = key || "e";
			var self = this;
			new inweo.core.event(this).addEvent("keydown", function(event) {
				if ($type(self.widget.getElement("input")) == "element") {
					return;
				}
				if (event.key == self.editKey) {
					event.preventDefault();
					self.edit();
				}
			});
		},
		href: function(url) {
			if (url != null) {
				this.widget.setProperty("href", url);
				this.widget.addClass("labelLink");
			} else {
				return this.widget.getProperty("href");
			}
		},
		text: function(string) {
			if (string != null) {
				return this.span.setText(string);
			} else {
				return this.span.getText() || null;
			}
		},
		initialize: function() {
			this.widget = new Element("a");
			this.widget.addClass("label");
			this.span = new Element("span");
			this.span.injectInside(this.widget);
			this.registerWidget("label");
		},
		lang: function(string) {
			if (string != null) {
				return this.widget.setProperty("lang", string);
			} else {
				return this.widget.getProperty() || null;
			}
		}	
	});
	inweo.widgets.label.tag = inweo.core.tag.extend({
		initialize: function() {
			inweo.loadCss(inweo.get("path") + "widgets/label/label.css");
		},
		insert: function(element, obj) {
			obj.text(element.getText());
			this.parent(element, obj);
		}
	});
}